package net.borderwars.map;

import net.borderwars.util.DoubleBuffer;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collection;

/**
 * @author Eric
 *         Date: Feb 25, 2005
 *         Time: 3:34:51 PM
 */
public class MapPane extends DoubleBuffer {
    Map ourMap = null;
    Rectangle currentDrawRectangle;

    public MapPane (Map m) {
        this.ourMap = m;

    }

    public void paintBuffer (Graphics g) {
        currentDrawRectangle = g.getClipBounds ();
        draw (g);
    }

    public int transY (int y) {
        //  int ny = (int) ((double) y * ((double) currentDrawRectangle.height / (double) ourMap.getHeight()));
        // int ny = currentDrawRectangle.height - y;
        return (y);
    }

    public int transX (int x) {
        //  int nx = (int) ((double) x * ((double) currentDrawRectangle.width / (double) ourMap.getWidth()));
        return (x);
    }

    BufferedImage bi = new BufferedImage (1000, 1000, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics g = bi.getGraphics ();

    public void draw (Graphics gg) {

        g.setColor (Color.BLACK);
        g.fillRect (0, 0, 1000, 1000);

        g.setColor (Color.GREEN);
        Collection<Mappable> mapped = ourMap.getMappables ();

        synchronized (ourMap) {
            for (Mappable item : mapped) {
                int nx = transX (item.loc_x ());
                int ny = transY (item.loc_y ());
                item.draw (nx, ny, g);

            }
        }
        //Rectangle r = gg.getClipBounds();
        gg.drawImage (bi, 0, 0, 500, 500, 0, 1000, 1000, 0, null);
    }
}
